/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;

/**
 *
 * @author root
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        // TODO code application logic here

        String lUserInput = "want a butter";

        QueryForm lQF = null;
        LexiconParser lLex = null;
        KnowledgeBase lKB = null;
        ChartParser lCP = null;

        try {
            lLex = new LexiconParser();
            lKB = lLex.load("src/ai/rules.txt");
            lCP = new ChartParser(lKB);


        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


        Jdbc j= null;
        Connection conn =null;
        try {
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            String url = "jdbc:oracle:thin:@tycho.ita.chalmers.se:1521/kingu.ita.chalmers.se";
            String userName = "TDA357_029"; // Your username goes here!
            String password = "B5TR5s"; // Your password goes here!
            conn = DriverManager.getConnection(url, userName, password);

            lQF = new QueryForm(conn);
             j = new Jdbc(lQF);
        //System.out.println(j.checkAnIncIngrediens(conn));
        //j.finalMeal(conn);

        
        //String student = args[0]; // This is the identifier for the student.

        } catch (SQLException e) {
            System.err.println(e);
            System.exit(2);
        }





        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What can I do for you?");
        String mode = "";
        System.out.print("> ");
        mode = input.readLine();
        while (!(mode.equals("no"))) {
            System.out.print(mode);
            

            lUserInput = mode;

            parseUsrInput(lUserInput, lLex, lKB, lCP, lQF);
            
            int x = lQF.getFinalMeals().size();
            for (int i = 0; i < lQF.getFinalMeals().size(); i++) {
                System.out.println(lQF.getFinalMeals().get(i));
            }

            
            switch(lQF.getLastEntry()){
                case QueryForm.INC:
                {
                    String lReply = "I found " + x + " meals containing ";
                    for(String s : lQF.getIncIngrediens()) {
                        if (!s.equals(lQF.getIncIngrediens().getLast())) {
                            lReply += s + " and ";
                        } else {
                            lReply += s + ".\n";
                        }

                    }
                    System.out.print(lReply);
                    break;
                }
                case QueryForm.EXC:
                {
                    String lReply = "I found " + x + " meals not containing ";
                    for(String s : lQF.getExcIngrediens()) {
                        if (!s.equals(lQF.getExcIngrediens().getLast())) {
                            lReply += s + " and ";
                        } else {
                            lReply += s + ".\n";
                        }

                    }
                    System.out.print(lReply);
                    break;
                }
            }
                        
            System.out.print("Is there anything else you whould like?\n");
            System.out.print("> ");
            mode = input.readLine();
        }
        j.finalMeal(conn);
        
    }

    public static void parseUsrInput(String pUserInput, LexiconParser pLex, KnowledgeBase pKB, ChartParser pCP, QueryForm pQF) {
        try {

            ParseNode lParseTree = pCP.parse(pUserInput);


            if (lParseTree != null) {
                System.out.println(lParseTree);
                lParseTree.executeSemantics(pKB, pQF);
            //System.out.println(pQF);
            }
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
